<?php
ob_start();

if (isset($_COOKIE['try']) && $_COOKIE['try'] >= 5) {
  redirect("?action=msg&header=header.error.login&body=error.login&view=login");
} else
  if (loginUser($_POST['login'], $_POST['pass'])) {
    if ($_POST['save'] == "on") {
      setcookie("login", $_POST['login'], time() + 60 * 60 * 24 * 20);
      setcookie("pass", $_POST['pass'], time() + 60 * 60 * 24 * 20);
    }
    setcookie("try");
    redirect("?action=msg&header=header.login.success&body=body.login.success&view=index");
  } else {
    if (isset($_COOKIE['try'])) {
      setcookie("try", $_COOKIE['try'] + 1, time() + 60 * 15);
    } else {
      setcookie("try", 1, time() + 60 * 15);
    }
    $initiatorAction = \controller\ControllerUtil::getEncodedActionFromUrlWithParameters($_SERVER['HTTP_REFERER']);
    redirect("?action=msg&header=header.login.failed&body=body.login.failed&view=" . $initiatorAction);
  }
ob_flush();
